//
//  ZHZPunchctry.m
//  ZHZtest
//
//  Created by 李彦宏 on 2017/12/12.
//  Copyright © 2017年 LYH. All rights reserved.
//

#import "JWZHZStudentsctry.h"

#import "NewStudentSearchHeard.h"//搜索头
#import "JWSDtabcell.h"//我的学员cell
#import "JWUserManager.h"
#import "SDUserModel.h"

#import "JWStudentSearCtry.h"//搜索界面
#import "JWStudentOptionsVC.h"//学员操作界面
#import "EBDropdownListView.h"//下拉选择列表



@interface JWZHZStudentsctry ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) SDUserModel *model;
@property(nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *contentArry;
@property (nonatomic, strong) NewStudentSearchHeard *HeadView;
@property(nonatomic,assign)NSInteger pageNum;
@end

@implementation JWZHZStudentsctry
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self setdata];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    [self setHeadView];
    self.model=[JWUserManager SDme];
    self.pageNum = 1;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageNum = 1;
        [self setdata];
        [self.HeadView removeFromSuperview];
        [self setHeadView];
        
    }];
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.pageNum ++;
        [self setdata];
    }];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark cell的内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ 
    JWSDtabcell *cell=[JWSDtabcell cellwithTableview:tableView];
    
    cell.model=self.contentArry[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    JWStudentOptionsVC *Stinfo=[[JWStudentOptionsVC alloc]init];
    self.model=self.contentArry[indexPath.row];
    Stinfo.title=[NSString stringWithFormat:@"%@的详情",self.model.name];
    
    [JWUserManager saveSD:self.model];
    [self.navigationController pushViewController:Stinfo animated:YES];
}
#pragma mark cell的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.contentArry.count;
}
#pragma mark section的个数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  1;
}

#pragma head的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
    
}

#pragma 隐藏UITableViewStyleGrouped下边多余的间隔
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
    
}

- (void)setHeadView{
    NewStudentSearchHeard *head=[[NewStudentSearchHeard alloc]initWithFrame:CGRectMake(0, Navigation_Bar_Height, ScreenWidth, 70)];
    self.HeadView=head;
    [self.view addSubview:head];
    [head.SearchBtn addTarget:self action:@selector(SearAction:) forControlEvents:UIControlEventTouchUpInside];
    __weak typeof(self) weakslf = self;
    //指定任务的执行方法--异步
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [head.MathdropdownListView setDropdownListViewSelectedBlock:^(EBDropdownListView *dropdownListView) {
            NSLog(@"----%@",dropdownListView.selectedItem.itemName);
            NSDictionary *dic=@{
                                @"jwid":JWid,
                                @"jwpassword":JWpassword,
                                @"fenlei":dropdownListView.selectedItem.itemName,
                                };
            [[AFNetworkTool shareManager]postJSONWithUrl:JWSDsearch_feilei parmas:dic successData:^(id json) {
                NSArray *arry=json[@"studentlist"];
                if(arry.count==0){
                    [MBProgressHUD showError:[NSString stringWithFormat:@"没有%@的学生",dropdownListView.selectedItem.itemName]];
                }else{
                    [weakslf.contentArry removeAllObjects];
                    for(int i=0;i<arry.count;i++){
                        self.model = [SDUserModel yy_modelWithJSON:arry[i]];
                        [JWUserManager saveSD:self.model];
                        [weakslf.contentArry addObject: self.model ];
                    }
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakslf.tableView reloadData];
                });
            } failure:^(NSError *error) {
                NSLog(@"--error--%@", error);
            }];
        }];
        [head.EnglishdropdownListView setDropdownListViewSelectedBlock:^(EBDropdownListView *dropdownListView) {
            NSDictionary *dic=@{
                                @"jwid":JWid,
                                @"jwpassword":JWpassword,
                                @"fenlei":dropdownListView.selectedItem.itemName,
                                };
            [[AFNetworkTool shareManager]postJSONWithUrl:JWSDsearch_feilei parmas:dic successData:^(id json) {
                NSArray *arry=json[@"studentlist"];
                if(arry.count==0){
                    [MBProgressHUD showError:[NSString stringWithFormat:@"没有%@的学生",dropdownListView.selectedItem.itemName]];
                }else{
                    [weakslf.contentArry removeAllObjects];
                    for(int i=0;i<arry.count;i++){
                        self.model = [SDUserModel yy_modelWithJSON:arry[i]];
                        [JWUserManager saveSD:self.model];
                        [weakslf.contentArry addObject: self.model ];
                    }
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakslf.tableView reloadData];
                });
            } failure:^(NSError *error) {
                NSLog(@"--error--%@", error);
            }];
        }];
    });
    
}
#pragma -mark ——————————————————搜索学生————————————————————
-(void)SearAction:(UIButton *)send
{
    
    JWStudentSearCtry *vc=[[JWStudentSearCtry alloc]init];
    //热搜
    vc.tagsArray=[[NSMutableArray alloc]init];
    if(_contentArry.count<7){
        vc.tagsArray=[NSMutableArray arrayWithObjects:@"李彦宏",@"朱圳",@"严戈闲",@"赵延",@"田涛",@"肖欢欣",@"游鹤亭",@"陈炽", nil];
    }else{

        for (int i=0; i<7; i++) {
            self.model =_contentArry[i];
            [vc.tagsArray addObject:self.model.name];
        }
    }
    [self.navigationController pushViewController:vc animated:YES];
}
-(void)setdata{
    
    NSDictionary *dict=@{
                         @"jwid" : JWid,
                         @"jwpassword" : JWpassword,
                         @"page" : @(self.pageNum),
                         };
    [[AFNetworkTool shareManager]getJSONWithUrl:JWteacher_Studentlist parame:dict successData:^(id json) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        if (self.pageNum == 1) {
            [self.contentArry removeAllObjects];
        }
        NSArray * arr = json[@"data"];
        if (arr.count == 0) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        __weak typeof(self) weakslf = self;
        for(int i=0;i<arr.count;i++){
            self.model = [SDUserModel yy_modelWithJSON:arr[i]];
            [JWUserManager saveSD:self.model];
            [weakslf.contentArry addObject: self.model ];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakslf.tableView reloadData];
        });
    } failure:^(NSError *error) {
        NSLog(@"--error--%@", error);
        [Utility showFail:@"网络连接错误..."];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSMutableArray *)contentArry {
    
    if(!_contentArry){
        _contentArry = [NSMutableArray array];
    }
    return _contentArry;
}
//estimatedRowHeight estimatedSectionHeaderHeight estimatedSectionFooterHeight
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 70+Navigation_Bar_Height, ScreenWidth, ScreenHeight-70-Navigation_Bar_Height)style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.tableFooterView=[[UIView alloc]init];
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.rowHeight=45;
        _tableView.estimatedRowHeight=0;
        _tableView.estimatedSectionHeaderHeight=0;
        _tableView.estimatedSectionFooterHeight=0;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
@end
